<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" plain icon="el-icon-refresh" @click="tableRefresh"></el-button>
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyWord" placeholder="账号姓名昵称包含关键词" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTableGdshop ref="table" :apiObj="apiObj" @selection-change="selectionChange" :column="tableColumn" stripe remoteSort remoteFilter>
				<el-table-column type="selection" width="50"></el-table-column>

				<template #status="scope">
					<el-tooltip
						effect="dark"
						content="点击切换状态"
						placement="left"
					>
						<el-button
							type="success"
							plain
							v-if="scope.row.status == 1"
							@click="setStatus(scope.row)"
						>正常</el-button
						>
						<el-button
							type="warning"
							plain
							v-else
							@click="setStatus(scope.row)"
						>禁用</el-button
						>
					</el-tooltip>
				</template>

				<el-table-column label="操作" fixed="right" align="right" width="220">
					<template #default="scope">
						<el-button type="text" size="small" @click="rowReSetPwd(scope.row)">重置密码</el-button>
<!--						<el-button type="text" size="small" @click="rowReSetPwd(scope.row)">积分</el-button>-->
<!--						<el-button type="text" size="small" @click="rowReSetPwd(scope.row)">余额</el-button>-->
						<el-button type="text" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
					</template>
				</el-table-column>

			</scTableGdshop>
		</el-main>

		<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
	</el-container>
</template>

<script>
import saveDialog from './infoEdit'

export default {
	name: "member-manage",
	components: {
		saveDialog
	},
	data() {
		return {
			apiObj: this.$API.member.member,
			selection: [],
			dialog: {
				save: false
			},
			search: {
				keyWord: '',
			},
			tableColumn: [
				{
					prop: "mobile",
					label: "账号",
					emptyText: "-",
				},
				{
					prop: "realname",
					label: "姓名",
					emptyText: "-",
				},
				{
					prop: "nickname",
					label: "昵称",
				},
				{
					prop: "integral_coin",
					label: "积分",
					emptyText: "0",
				},
				{
					prop: "money_coin",
					label: "余额",
					formatter: (row, column, cellValue) => {
						return '￥' + this.$TOOL.getMoneyByMinute(cellValue)
					}
				},
				{
					prop: "remark",
					label: "备注",
				},
				{
					prop: "status",
					label: "状态",
					width: 100
				},
			]
		};
	},
	methods: {
		tableRefresh() {
			this.$refs.table.refresh()
		},
		setStatus(_item) {
			const that = this;
			that.$confirm("此操作将切换当前账号的状态, 是否继续?", "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			})
				.then(() => {
					that.$API.member.member
						.setStatus({ ids: [_item.id] })
						.then((res) => {
							if (res.code === 0) {
								that.$message({
									type: "success",
									message: "切换成功"
								});
								_item.status = !_item.status;
							}
							else {
								this.$message({
									type: "error",
									message: "失败," + res.message
								});
							}
						});
				})
				.catch(() => {});
		},
		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		//搜索
		upsearch(){
			this.$refs.table.upData(this.search)
		},
		add() {

		},
		rowReSetPwd(_row) {
			console.log(_row);
			const that = this;
			that.$confirm("确认重置《" + _row.mobile + "》的密码吗？", "提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			})
				.then(() => {
					that.$API.member.member
						.reSetPwd({ id: _row.id })
						.then((res) => {
							if (res.code === 0) {
								this.$message({
									type: "success",
									message: "重置成功"
								});
							}
							else {
								this.$message({
									type: "error",
									message: "失败," + res.message
								});
							}
						});
				})
				.catch(() => {});
		},
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		handleSuccess(data, mode){
			console.log(data, mode);
			this.tableRefresh();
		},
	}
}
</script>

<style scoped>

</style>
